
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="商品图片:" prop="shopUrl">
    <el-input v-model="formData.shopUrl" :clearable="true" placeholder="请输入商品图片" />
</el-form-item>
        <el-form-item label="商品名称:" prop="shopName">
    <el-input v-model="formData.shopName" :clearable="true" placeholder="请输入商品名称" />
</el-form-item>
        <el-form-item label="商品价格:" prop="shopPrice">
    <el-input-number v-model="formData.shopPrice" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="商品货号:" prop="shopItemNumber">
    <el-input v-model.number="formData.shopItemNumber" :clearable="true" placeholder="请输入商品货号" />
</el-form-item>
        <el-form-item label="商品标题:" prop="shopTitle">
    <el-input v-model="formData.shopTitle" :clearable="true" placeholder="请输入商品标题" />
</el-form-item>
        <el-form-item label="商品库存:" prop="shopInventory">
    <el-input v-model="formData.shopInventory" :clearable="true" placeholder="请输入商品库存" />
</el-form-item>
        <el-form-item label="商品销量:" prop="shopSales">
    <el-input v-model="formData.shopSales" :clearable="true" placeholder="请输入商品销量" />
</el-form-item>
        <el-form-item label="商品状态:" prop="shopStatus">
    <el-select v-model="formData.shopStatus" placeholder="请选择商品状态" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in shop_statusOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="商品详情:" prop="shopDetails">
    <RichEdit v-model="formData.shopDetails"/>
</el-form-item>
        <el-form-item label="商品属性:" prop="shopAttribute">
    <el-input v-model="formData.shopAttribute" :clearable="true" placeholder="请输入商品属性" />
</el-form-item>
        <el-form-item label="商品限购:" prop="shopLimit">
    <el-select v-model="formData.shopLimit" placeholder="请选择商品限购" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in shop_limitOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="配送方式:" prop="shopDelivery">
    <el-select v-model="formData.shopDelivery" placeholder="请选择配送方式" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in shop_deliveryOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="运费模版:" prop="shopShippingCost">
    <el-select v-model="formData.shopShippingCost" placeholder="请选择运费模版" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in shop_shipping_costOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="物流模版:" prop="shopLogistics">
    <el-select v-model="formData.shopLogistics" placeholder="请选择物流模版" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in shop_logisticsOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createLsShop,
  updateLsShop,
  findLsShop
} from '@/api/shop/lsShop'

defineOptions({
    name: 'LsShopForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
// 富文本组件
import RichEdit from '@/components/richtext/rich-edit.vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const shop_statusOptions = ref([])
const shop_deliveryOptions = ref([])
const shop_shipping_costOptions = ref([])
const shop_logisticsOptions = ref([])
const shop_limitOptions = ref([])
const formData = ref({
            shopUrl: '',
            shopName: '',
            shopPrice: 0,
            shopItemNumber: undefined,
            shopTitle: '',
            shopInventory: '',
            shopSales: '',
            shopStatus: '',
            shopDetails: '',
            shopAttribute: '',
            shopLimit: '',
            shopDelivery: '',
            shopShippingCost: '',
            shopLogistics: '',
        })
// 验证规则
const rule = reactive({
               shopUrl : [{
                   required: true,
                   message: '请选择商品图片',
                   trigger: ['input','blur'],
               }],
               shopName : [{
                   required: true,
                   message: '请输入商品名称',
                   trigger: ['input','blur'],
               }],
               shopPrice : [{
                   required: true,
                   message: '请输入商品价格',
                   trigger: ['input','blur'],
               }],
               shopItemNumber : [{
                   required: true,
                   message: '请输入商品货号',
                   trigger: ['input','blur'],
               }],
               shopTitle : [{
                   required: true,
                   message: '请输入商品标题',
                   trigger: ['input','blur'],
               }],
               shopInventory : [{
                   required: true,
                   message: '请输入仓科库存',
                   trigger: ['input','blur'],
               }],
               shopSales : [{
                   required: true,
                   message: '请输入商品销量',
                   trigger: ['input','blur'],
               }],
               shopStatus : [{
                   required: true,
                   message: '请输入商品状态',
                   trigger: ['input','blur'],
               }],
               shopDetails : [{
                   required: true,
                   message: '请输入商品详情',
                   trigger: ['input','blur'],
               }],
               shopAttribute : [{
                   required: true,
                   message: '请输入商品属性',
                   trigger: ['input','blur'],
               }],
               shopLimit : [{
                   required: true,
                   message: '请选择是否限购',
                   trigger: ['input','blur'],
               }],
               shopDelivery : [{
                   required: true,
                   message: '请选择配送方式',
                   trigger: ['input','blur'],
               }],
               shopShippingCost : [{
                   required: true,
                   message: '请输入运费模版',
                   trigger: ['input','blur'],
               }],
               shopLogistics : [{
                   required: true,
                   message: '请选择物流模版',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findLsShop({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    shop_statusOptions.value = await getDictFunc('shop_status')
    shop_deliveryOptions.value = await getDictFunc('shop_delivery')
    shop_shipping_costOptions.value = await getDictFunc('shop_shipping_cost')
    shop_logisticsOptions.value = await getDictFunc('shop_logistics')
    shop_limitOptions.value = await getDictFunc('shop_limit')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createLsShop(formData.value)
               break
             case 'update':
               res = await updateLsShop(formData.value)
               break
             default:
               res = await createLsShop(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
